#include <unistd.h>
#include <stdio.h>
#include <errno.h>

#define BUFFER_LEN 255
int main(int argc, char **argv)
{
	char buffer[BUFFER_LEN+1];
	int fd[2];

	if(argc != 2)
	{
		fprintf(stderr, "Useage: %s string \n\a", argv[0]);
		exit(1);
	}

	if(pipe(fd) != 0)
	{
		fprintf(stderr, "Pipe error: %s\n\a", strerror(errno));
		exit(1);
	}

	if(fork() == 0)
	{
		close(fd[0]);
		printf("Child process %d write to pipe\n\a", getpid());
		snprintf(buffer, BUFFER_LEN, argv[1]);
		write(fd[1],buffer,strlen(buffer));
		printf("Child process %d exit\n\a", getpid());
		exit(0);
	}
	else
	{
		close(fd[1]);
		printf("Parent process %d read from pipe\n\a", getpid());
		memset(buffer, 0x00, sizeof(buffer));
		read(fd[0], buffer, BUFFER_LEN);
		printf("Parent process %d read: %s\n", getpid(), buffer);
		exit(1);
	}

	return 0;
}
